package com.eos.control.action.emp;

import com.eos.business.service.SysUserBusinessService;
import com.eos.common.tool.DataCacheUtil;
import com.eos.common.tool.ObjectUtils;
import com.eos.control.action.base.AbstractBaseAction;
import com.eos.orm.mybatis.model.SysUser;

public class UpdateEmpAction extends AbstractBaseAction {
	private static final long serialVersionUID = -9055218404166521444L;
	private Integer sysUserId;
	private String userName;
	private String userEmail;
	private String oldUserEmail;
	private String telephone;
	private String userMobile;
	private String userDesc;
	private String status;
	private Integer roleId;

	public void setSysUserId(Integer sysUserId) {
		this.sysUserId = sysUserId;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public void setUserEmail(String userEmail) {
		this.userEmail = userEmail;
	}

	public void setOldUserEmail(String oldUserEmail) {
		this.oldUserEmail = oldUserEmail;
	}

	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}

	public void setUserMobile(String userMobile) {
		this.userMobile = userMobile;
	}

	public void setUserDesc(String userDesc) {
		this.userDesc = userDesc;
	}

	public void setRoleId(Integer roleId) {
		this.roleId = roleId;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	@Override
	public String execute() {
		try {
			if (null == sysUserId || sysUserId.intValue() <= 0) {
				statusCode = "300";
				message = "参数错误,保存用户信息失败!";
				return SUCCESS;
			} 
			SysUser su = DataCacheUtil.getSysUser(userEmail);
			if(null != su && !userEmail.equals(oldUserEmail)){
				statusCode = "300";
				message = "你输入的邮箱已被占用, 请换一个再试!";
			}else{
				SysUser user = new SysUser();
				user.setId(sysUserId);
				user.setModifyTime(ObjectUtils.formatDate(null, null));
				user.setRoleId(roleId);
				user.setStatus(status);
				user.setUserDesc(userDesc);
				user.setUserEmail(userEmail);
				user.setUserMobile(userMobile);
				user.setUserName(userName);
				user.setUserTelephone(telephone);
				user.setRoleId(roleId == null ? 0 : roleId);
				SysUserBusinessService sbs = new SysUserBusinessService();
				String flag = sbs.update(user);
				if("SUCCESS".equals(flag)){
					statusCode = "200";
					message = "保存用户信息成功!";
					DataCacheUtil.clearSysUserMap();
				}else{
					statusCode = "300";
					message = "系统繁忙,修改失败!";
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			statusCode = "300";
			message = "系统异常,保存用户信息失败!";
		}
		return SUCCESS;
	}
}
